package com.medical.controller;

import com.medical.model.entity.Employee;
import com.medical.service.IEmployeeService;
import com.medical.domain.R;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    private final IEmployeeService employeeService;

    public EmployeeController(IEmployeeService employeeService) {
        this.employeeService = employeeService;
    }

    // 新增员工
    @PostMapping
    public R<Boolean> addEmployee(@RequestBody Employee employee) {
        return R.ok(employeeService.save(employee));
    }

    // 删除员工
    @DeleteMapping("/{id}")
    public R<Boolean> deleteEmployee(@PathVariable Long id) {
        return R.ok(employeeService.removeById(id));
    }

    // 更新员工
    @PutMapping
    public R<Boolean> updateEmployee(@RequestBody Employee employee) {
        return R.ok(employeeService.updateById(employee));
    }

    // 查询单个员工（带部门名称）
    @GetMapping("/{id}")
    public R<Employee> getEmployee(@PathVariable Long id) {
        Employee ro = employeeService.getEmployeeWithDeptById(id);
        return ro != null ? R.ok(ro) : R.fail(1001,"员工不存在");
    }

    // 分页查询所有员工（带部门名称）
    @GetMapping("/list")
    public R<List<Employee>> listEmployees() {
        return R.ok(employeeService.listEmployeesWithDept());
    }
}